﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using OnlineVideos.Data;
using Common.Library;
using PicasaMobileInterface;
using System.Windows.Threading;
using OnlineVideos.Entities;


namespace OnlineVideos.Views
{
    public partial class UserSharePAge : PhoneApplicationPage
    {
        public string catname = string.Empty;
        public UserSharePAge()
        {
            InitializeComponent();
            Loaded += UserSharePAge_Loaded;
        }

        void UserSharePAge_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> catlist=new List<string>();
            catlist.Add("Select Category");

            foreach (string cat in Constants.connection.Table<BlogCategoryTable>().ToListAsync().Result.Select(i => i.BlogCategory).ToList())
            {
                catlist.Add(cat);
            }
            CategoryList.ItemsSource = catlist;
            CategoryList.SelectionChanged += CategoryList_SelectionChanged_1;
        }

       

        private void Share_Click(object sender, RoutedEventArgs e)
        {
            if (CategoryList.Items.Count > 0 && CategoryList.SelectedIndex != 0)
            {
                CatVal.Visibility = Visibility.Collapsed;
                PicasaMobileInterface.UploadToBlog ub = new PicasaMobileInterface.UploadToBlog(AppSettings.ShowUniqueID, catname, tbDes.Text);
                Sharestk.Visibility = Visibility.Collapsed;
                ShareTB.Visibility = Visibility.Visible;
                DispatcherTimer dt = new DispatcherTimer();
                dt.Interval = TimeSpan.FromSeconds(3);
                dt.Tick += dt_Tick;
                dt.Start();
            }
            else
            {
                CatVal.Visibility = Visibility.Visible;
            }
           
        }

        void dt_Tick(object sender, EventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                (sender as DispatcherTimer).Stop();
                NavigationService.GoBack();
            });
        }

        private void CategoryList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if(CategoryList.ItemsSource!=null)
            catname = (sender as ListPicker).SelectedItem.ToString();
        }

        private void ShareVideoMix_Click(object sender, EventArgs e)
        {
            if (CategoryList.Items.Count > 0 && CategoryList.SelectedIndex != 0)
            {
                CatVal.Visibility = Visibility.Collapsed;
                PicasaMobileInterface.UploadToBlog ub = new PicasaMobileInterface.UploadToBlog(AppSettings.ShowUniqueID, catname, tbDes.Text);
                Sharestk.Visibility = Visibility.Collapsed;
                ShareTB.Visibility = Visibility.Visible;
                DispatcherTimer dt = new DispatcherTimer();
                dt.Interval = TimeSpan.FromSeconds(3);
                dt.Tick += dt_Tick;
                dt.Start();
            }
            else
            {
                CatVal.Visibility = Visibility.Visible;
            }
        }
        private void TextBlock_Loaded_1(object sender, RoutedEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb.Text == "Select Category")
            {
                tb.Visibility = Visibility.Collapsed;
                tb.Loaded -= TextBlock_Loaded_1;
            }
        }
    }
}